//
//  JMAutonomyViewController.m
//  VoluntaryStar
//
//  Created by ios on 15/8/27.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMAutonomyViewController.h"
#include "JMBrowserViewController.h"
#import "JMCoustmButton.h"
#import "JMAcademyViewController.h"
#import "JMZujiViewController.h"
#import "JMEnrollmentPolicyViewController.h"
#import "JMProfessionalSettingViewController.h"


#import "JMLoginViewController.h"



@interface JMAutonomyViewController ()


@property (nonatomic,strong) UIImageView *backImageView;
@property (nonatomic,strong) NSMutableArray *itemButtonArray;

@end

@implementation JMAutonomyViewController

-(UIImageView *)backImageView{
    if(!_backImageView){
        _backImageView = [[UIImageView alloc]init];
        _backImageView.backgroundColor = [UIColor clearColor];
        _backImageView.image = [UIImage imageNamed:@"secondLevel.bundle/zizhu/zhaosheng_backImage"];
        _backImageView.frame = CGRectMake(0, 0, UI_SCREEN_WITH, UI_SCREEN_HEIGHT);
    }
    return _backImageView;
}
-(NSMutableArray *)itemButtonArray{
    if(!_itemButtonArray){
        _itemButtonArray = [[NSMutableArray alloc]initWithCapacity:0];
        NSArray *array = @[@"secondLevel.bundle/zizhu/yuanxiao_image",
                           @"secondLevel.bundle/zizhu/set_image",
                           @"secondLevel.bundle/zizhu/zhengce_image",
                           @"secondLevel.bundle/zizhu/cehngzhang_@2x",
                           @"secondLevel.bundle/zizhu/wenda_image"];
        for(int i =0 ;i <array.count;i++){
            JMCoustmButton *btn = [[JMCoustmButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0) WithNormalImage:array[i] SelectedImage:@""];
            btn.tag = i;
            [btn addTarget:self action:@selector(itemButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [_itemButtonArray addObject:btn];
        }
    }
    return _itemButtonArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
    self.navBackBtn.hidden =YES;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.backImageView];
    [self setItemButtonFrame];
}

-(void)setItemButtonFrame{
    for(int  i =0 ;i<self.itemButtonArray.count;i++){
        JMCoustmButton *btn = self.itemButtonArray[i];
        if(i != 4){
            if(iphone4){
                CGFloat poinx= (UI_SCREEN_WITH - UI_SCREEN_WITH *0.3375 *0.75 *2)/3.0;
                btn.frame = CGRectMake(poinx + (poinx + UI_SCREEN_WITH *0.3375*0.75 )*(i%2),
                                       UI_SCREEN_HEIGHT *0.099225 + (UI_SCREEN_WITH *0.353125 + UI_SCREEN_HEIGHT *0.044014) * (i/2),
                                       UI_SCREEN_WITH *0.3375 *0.75,
                                       UI_SCREEN_WITH *0.353125 *0.75);

            }else{
                btn.frame = CGRectMake(UI_SCREEN_WITH *0.109375 + (UI_SCREEN_WITH *0.109375 + UI_SCREEN_WITH *0.3375 )*(i%2),
                                       UI_SCREEN_HEIGHT *0.099225 + (UI_SCREEN_WITH *0.353125 + UI_SCREEN_HEIGHT *0.044014) * (i/2),
                                       UI_SCREEN_WITH *0.3375,
                                       UI_SCREEN_WITH *0.353125);
            }
        }else{
            if(iphone4){
                btn.frame = CGRectMake(UI_SCREEN_WITH/2.0 -UI_SCREEN_WITH *0.3375 *0.75/2.0 ,
                                       UI_SCREEN_HEIGHT *0.079225 + (UI_SCREEN_WITH *0.353125 + UI_SCREEN_HEIGHT *0.044014) * (i/2),
                                       UI_SCREEN_WITH *0.3375 *0.75,
                                       UI_SCREEN_WITH *0.353125 *0.75);
            }else{
                btn.frame = CGRectMake(UI_SCREEN_WITH/2.0 -UI_SCREEN_WITH *0.3375/2.0 ,
                                       UI_SCREEN_HEIGHT *0.079225 + (UI_SCREEN_WITH *0.353125 + UI_SCREEN_HEIGHT *0.044014) * (i/2),
                                       UI_SCREEN_WITH *0.3375,
                                       UI_SCREEN_WITH *0.353125);
            }
        }
        [self.view addSubview:btn];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark  item 点击 进入 网页
-(void)itemButtonClick:(JMCoustmButton *)sender{
    
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if(![cf.user.state isEqualToString:@"1"]){
        JMLoginViewController *vc = [JMLoginViewController new];
        JMNavigationController *nav = [[JMNavigationController alloc]initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:^{
        }];
        return;
    }
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:cf.user.userId forKey:@"user"];
    [dict setObject:cf.user.token forKey:@"token"];
    [JMAppNetworkClient token_WithInfo:dict complete:^(id data, NSError *error) {
        if([data[@"status"] integerValue] == 1){
            if(sender.tag == 0){
                JMAcademyViewController *vc = [JMAcademyViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }else if(sender.tag == 1){
                JMProfessionalSettingViewController *vc =[JMProfessionalSettingViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }else if(sender.tag == 2){
                JMEnrollmentPolicyViewController *vc =[JMEnrollmentPolicyViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }
            else if(sender.tag == 3){
                JMZujiViewController *vc =[JMZujiViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                JMBrowserViewController *vc =[JMBrowserViewController new];
                vc.url =[NSURL URLWithString:[JMAppNetworkClient independentCandidatesAnswerUrl]];
                [self.navigationController pushViewController:vc animated:NO];
            }
        }else{
            [JMTools showAlertView:self.view message:data[@"message"]];
            [self gotoLogin];
        }
    }];

}

-(void)gotoLogin{
    JMLoginViewController *vc = [JMLoginViewController new];
    JMNavigationController *nav = [[JMNavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:^{
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
